<template>
  <div class="app-container">
    <h4>基本信息</h4>
    <div class="change">
      <p>
        <span>借用人</span>
        <el-input placeholder="请选择借用人" :disabled="islook" v-model="tempData.borrowPerson" class="userselect">
          <el-button slot="append" icon="el-icon-s-custom" :disabled="islook" @click="checkBuyer2"></el-button>
        </el-input>
      </p>

      <p>
        <span>借出时间</span>
        <el-date-picker
          v-model="tempData.borrowTime"
          type="date"
          value-format="yyyy-MM-dd"
          class="userselect"
          placeholder="选择日期"
        ></el-date-picker>
      </p>

      <p>
        <span>预计归还时间</span>
        <el-date-picker
          v-model="tempData.predictReturnTime"
          type="date"
          value-format="yyyy-MM-dd"
          class="userselect"
          placeholder="选择日期"
        ></el-date-picker>
      </p>
    </div>

    <h4>选择资产</h4>
    <div class="zd3-btm">
      <div class="addDate" @click="addzichan" v-if="!islook">+ 选择资产</div>
      <el-table :data="proData" :show-header="true" style="width: 100%;border-radius: 8px;">
        <el-table-column type="index" align="center" width="50" label="序号" fixed="left" />
        <el-table-column align="center" prop="propertySn" width="150" label="资产SN编码" fixed="left" />
        <el-table-column align="center" prop="propertyName" width="150" label="资产名称" />
        <el-table-column align="center" prop="propertyType" width="150" label="资产类别" />
        <el-table-column align="center" prop="belongCompany" width="150" label="当前所在公司" />
        <el-table-column align="center" prop="departments" width="150" label="当前所在部门" />
        <el-table-column align="center" prop="usePerson" width="150" label="当前使用人" />
        <el-table-column align="center" prop="storeAreas" label="存放地点" />
        <el-table-column align="center" width="100" label="操作" fixed="right" v-if="!islook">
          <template slot-scope="scope">
            <el-popconfirm
              placement="top"
              hideIcon
              title="是否要删除此行？"
              @onConfirm="deleteRow(scope.row,scope.$index)"
            >
              <el-button type="text" size="small" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="submits" v-if="!islook">
      <div class="save" @click="saveOrder">确认借用</div>
      <div class="nosave" @click="nosaveOrder">取消</div>
    </div>

    <el-dialog title="选择资产" :visible.sync="addProShow" width="80%" top="20px">
      <div class="product">
        <div class="pro-r">
          <div class="zd3-title">
            <p>
              <span>资产名</span>
              <input v-model="propertyName" type="text" placeholder="请输入资产名" />
            </p>
            <el-button size="small" type="success" @click="searchList">查询</el-button>
            <el-button size="small" @click="resetList">重置</el-button>
          </div>
          <el-table
            ref="singleTable"
            :data="proDeail"
            border
            highlight-current-row
            @current-change="handleAdd"
            style="width: 100%"
            height="550"
          >
            <el-table-column align="center" width="100" label="资产状态" fixed="left">
              <template slot-scope="scope">
                <div class="orderStatus">
                  <p v-if="scope.row.status === '闲置'" class="yellow">闲置</p>
                  <p v-if="scope.row.status === '报废'" class="red">报废</p>
                  <p v-if="scope.row.status === '借用'" class="blue">借用</p>
                  <p v-if="scope.row.status === '使用中'" class="green">使用中</p>
                </div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="propertyName" label="资产名称" />
            <el-table-column align="center" prop="propertyType" label="资产类别" />
            <el-table-column align="center" prop="propertyGuige" label="规格型号" />
            <el-table-column align="center" prop="propertySn" label="SN号" />
            <el-table-column align="center" prop="propertyJine" label="金额" />
            <el-table-column align="center" prop="companys" label="使用公司" />
            <el-table-column align="center" prop="departments" label="使用部门" />
            <el-table-column align="center" prop="usePerson" label="使用人" />
            <el-table-column align="center" prop="belongCompany" label="所属公司" />
            <el-table-column align="center" prop="managerPerson" label="管理员" />
            <el-table-column align="center" prop="purchaseTime" label="购入日期" />
            <el-table-column align="center" prop="suppliers" label="供应商" />
            <!-- <el-table-column align="center" prop="number" label="现存量"/> -->
            <el-table-column align="center" prop="serviceLife" label="使用期限" />
            <el-table-column align="center" prop="sourceOf" label="来源" />
            <el-table-column align="center" prop="powers" label="区域名称" />
            <el-table-column align="center" prop="powers" label="存放地" />
          </el-table>
          <el-pagination
            background
            align="right"
            layout="prev, pager, next"
            :current-page="totalAssets"
            @current-change='goPageAssets'
            @prev-click="goPrevAssets"
            @next-click="goNextAssets"
          />
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="noxuanzhong">取 消</el-button>
        <el-button type="primary" @click="xuanzhong">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 领用处理人 -->
    <el-dialog title="选择处理人" :visible.sync="buyerShow2" width="80%" top="20px">
      <div class="files">
				<div class="files-left">
					<el-menu class="el-menu-vertical-demo" @select="handleSelece" default-active="0">
						<el-menu-item :index='index' v-for="(item,index) in ifydata" :key="index">
							<span slot="title">{{item.departments}}</span>
						</el-menu-item>
					</el-menu>
				</div>
				<div class="files-right">
					<div class="files-btm">
						<el-table 
              :data="peopleData" 
              height="400" 
              highlight-current-row
              @current-change="handlePeople2"
              style="margin-bottom: 20px;" 
              :show-header="true" >
							<el-table-column align="center" prop="username" label="姓名"></el-table-column>
							<el-table-column align="center" prop="departments" label="所属部门"></el-table-column>
							<el-table-column align="center" prop="sex" label="性别"></el-table-column>
							<el-table-column align="center" prop="power" label="职务"></el-table-column>
						</el-table>
						<el-pagination background hide-on-single-page :page-size="50" layout="prev, pager, next" :total="total" @prev-click="goPrev" @current-change="goPage" @next-click="goNext" />
					</div>
				</div>
			</div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="buyerShow = false">取 消</el-button>
        <el-button size="small" type="primary" @click="buyerShow = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getUser, getBumenNew, getUserFiles } from "@/api/people";
import { rukuList, deleteRuku,updateRuku, addJieyong } from "@/api/caigou";
let selectid = null,navindex = null;
export default {
  data() {
    return {
      radio: "1",
      list1: [],
      list2: [],
      total: 1,
      page: 1,
      totalAssets: 1,
      pageAssets: 1,
      value: "",
      search: {},
      search1: {},
      tableData: [],
      proData: [],
      oneClassify: [], //一级分类
      twoClassify: [],
      tempData: {
        borrowPerson: "",
        borrowTime: "",
        predictReturnTime: "",
        putList: "",
        status: "借用",
      },

      addProShow: false,
      ifydata: [], //部门列表
      proDeail: [], //商品列表
      peopleData: [], //人员列表
      currentRow: null,
      buyerShow2: false, //采购员弹窗状态
      proData: [],
      propertyName: '', //根据名称搜索设备
      islook: false,
    };
  },
  mounted() {
    if(this.$route.params.look){
      this.tempData = JSON.parse(this.$route.params.options)
      console.trace('tempData:',this.tempData)
      this.proData = this.tempData.putList
      this.islook = true
    }else{
      this.zichanlist();
      const userinfo = this.$store.state.user.userInfo[0]
      this.getpeople(userinfo.departments)
      this.bumenList()
    }
  },
  methods: {
    saveOrder() {
      if(!this.proData.length){
        console.log('meiyouxuanze')
        this.$message({
          message: '请选择资产！',
          type: 'warning'
        })
        return
      }
      //			新增借用
      this.proData.map(item => {
        delete item.updateTime
        delete item.createTime
        delete item.insertTime
        delete item.propertyType
        return item
      })
      this.tempData.departments = this.$store.state.user.userInfo[0].departments
      this.tempData.putList = this.proData;
      console.log(this.tempData);
      console.log(this.proData);
      this.tempData.putList.map(item => {
        item.status = '借用'
        return item
      })
      addJieyong(this.tempData).then((res) => {
        console.log(res);
        this.$message({
          message: res.message,
          type: "success",
        });
        this.$router.go(-1);
      });
    },
    // 资产分页
    goPrevAssets(page) {
      console.log(page)
      this.pageAssets--
      this.getAsset(selectid)
    },
    goNextAssets(page) {
      console.log(page)
      this.pageAssets++
      this.getAsset(selectid)
    },
    goPageAssets(page){
      console.log(page)
      this.pageAssets = page
      this.getAsset(selectid)
    },
    // 分页
    goPrev(page) {
      console.log(page)
      this.page--
      this.getpeople(navindex)
    },
    goNext(page) {
      console.log(page)
      this.page++
      this.getpeople(navindex)
    },
    goPage(page){
      console.log(page)
      this.page = page
      this.getpeople(navindex)
    },
     /**
     * 根据资产名搜索
     */
    searchList(){
      this.loading = true
      this.zichanlist()
    },
    /**
     * 重置名称搜索
     */
    resetList(){
      this.propertyName = null
    },
    zichanlist() {
      this.loading = true
      rukuList({
        departments: this.$store.state.user.userInfo[0].departments,
        propertyName: this.propertyName ? this.propertyName : null,
        currentPage: this.pageAssets
      }).then((res) => {
        console.log('借用归还展示数据',res);
        let temparr = res.data.list.filter(item => item.status === '闲置' || item.status === '使用中')
        this.totalAssets = res.data.pages
        this.proDeail = temparr
        this.loading = false
      });
    },

    addzichan() {
      this.addProShow = true;
      // rukuList(this.search).then((res) => {
      //   this.list1 = res.data.list;
      //   console.log(this.list1);
      // });
      // rukuList(this.search1).then((res) => {
      //   this.list2 = res.data.list;
      //   console.log(this.list2);
      // });
      // for (var i = 0; i < this.list2.length; i++) {
      //   this.list1.push(this.list2[i]);
      //   var list3 = this.list1;
      // }

      // this.proDeail = list3;
    },
    noxuanzhong() {
      this.addProShow = false;
    },

    //将选中的资产追加到proData数组中
    handleAdd(row) {
      console.log(row);
      row.num = 1;
      this.proData.push({ ...row });
      console.log(this.proData);
      this.addProShow = false
    },
    xuanzhong() {
      this.addProShow = false;
    },
    // 人员列表
    getpeople(id){
      console.log('id:',id)
      getUser({departments: id,currentPage: this.page}).then(res => {
        console.log(res)
        this.peopleData = res.data.list
        this.total = res.data.total
      })
    },
    // 选择人员分类
    handleSelece(index){
			console.log(index)
			console.log('ifydata:',this.ifydata[index])
			const navindex = this.ifydata[index].value ? this.ifydata[index].value : null
			this.getpeople(navindex)
    },
    // 选择报修人
    checkBuyer2() {
      this.buyerShow2 = true;
    },
    // 选择保修人员
    handlePeople2(row){
      console.log(row)
      this.tempData.borrowPerson = row.username
			this.buyerShow2 = false
		},
    // 部门列表
    bumenList() {
      getBumenNew({}).then((res) => {
        console.log("----------------");
        console.log(res);
        let temp = res.data.filter(item => item.ids === '456')[0].departmentsList
        console.log(temp)
        temp.map((item) => {
          item.value = item.departments;
          return item;
        });
        this.ifydata = temp;
      });
    },
    // 删除对应商品数据
    deleteRow(value, index) {
      console.log(index);
      this.proData.splice(index, 1);
    },
    // 取消新增
    nosaveOrder() {
      console.log("取消");
      this.$router.go(-1);
    },
  },
};
</script>
<style lang="scss" scoped>
/deep/.el-input-group__append button.el-button{
  padding: 8px 12px;
}
/deep/.el-dialog__body {
  padding: 0px 20px 20px 20px;
  color: #606266;
  font-size: 14px;
  word-break: break-all;
}
.orderStatus {
  display: flex;
	justify-content: center;
  > p {
    padding: 0px 5px;
    color: #fff;
    font-size: 12px;
    border-radius: 2px;
    margin: 0 0px 0 0;
  }
  .blue {
    background: #3a87ad;
  }
  .green {
    background: #18a05e;
  }
  .yellow {
    background: #ff7a0e;
  }
  .red {
    background: #c60000;
  }
  .zise{
    background: #c7ab10
  }
}
.popup {
  padding: 0 20px;
}
.btns {
  display: flex;
  align-items: center;
  margin-left: 10px;
  /*position: absolute;
	right: 20px;
	bottom: 5px;*/
  > h3 {
    width: 64px;
    height: 32px;
    background: #18a05e;
    border-radius: 3px;
    color: #fff;
    font-size: 14px;
    line-height: 32px;
    font-weight: 100;
    text-align: center;
    cursor: pointer;
  }
}
.product{
  // display: flex;
  .pro-l{
    // width: 200px;
    height: 70px;
    display: flex;
    align-items: center;
    padding: 0 20px;
    position: relative;
    background: #ffffff;
    border-radius: 8px;

    > p {
      font-size: 14px;
      color: #333;
      margin-right: 20px;
      > span {
        padding-right: 20px;
      }

      > input {
        width: 150px;
        height: 32px;
        // background: #ccc;
        border-radius: 3px;
        border: 1px solid #ccc;
        padding-left: 10px;
        color: #333;
      }
    }
  }
  .pro-r{
    flex: 1;
    overflow: hidden;
    /deep/.el-table--enable-row-hover .el-table__body tr:hover>td {
      background-color: rgba(24,160,94,.2);
    }
    /deep/.el-table tr{
      cursor: pointer;
    }
    /deep/.el-table__body tr.current-row>td {
      background-color: rgba(24,160,94,.4);
    }
  }
}
/deep/.el-dialog__body {
  padding: 10px 20px;
}
.zd3-title {
	height: 60px;
	display: flex;
	align-items: center;
	padding: 0 20px;
	position: relative;
	background: #ffffff;
	border-radius: 8px;
	// border: 1px solid rgba(0, 0, 0, 0.1);

	> p {
		font-size: 14px;
		color: #333;
		margin-right: 20px;

		> span {
			padding-right: 20px;
		}

		> input {
			width: 150px;
			height: 32px;
			// background: #ccc;
			border-radius: 3px;
			border: 1px solid #ccc;
			padding-left: 10px;
			color: #333;
		}
	}
}
.files {
  display: flex;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  margin-top: 20px;
  overflow: hidden;
  padding: 20px;
}
.files-btm .btn-prev:after {
  content: "上一页";
}
.files-btm .btn-next:before {
  content: "下一页";
}
.files-btm .btn-prev,
.files-btm .btn-next {
  width: 65px;
}
.files-left {
	width: 160px;
	margin-right: 20px;
  height: 420px;
  overflow-y: auto;
	/deep/.el-menu{
		border-right: none;
	}
	/deep/.el-menu-item{
		height: 35px;
		line-height: 35px;
		margin-bottom: 8px;
	}
	/deep/.el-menu-item.is-active{
		color: #fff;
		border-radius: 5px;
		background: rgba(24,160,94,.8);
	}
}
.files-right {
  flex: 1;
  overflow: hidden;
}
.files-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  // margin-top: 20px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 45px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.avatar {
  width: 30px;
  height: 30px;
}
.zd3-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 0 0 8px 8px;
  // border: 1px solid rgba(0, 0, 0, 0.1);
  // margin-top: 20px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 70px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.inps {
  width: 95%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: none;
  border: none;
  color: rgba(0, 0, 0, 0.8);
}
.inps-active {
  width: 95%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: rgba(24, 160, 94, 0.2);
  border: none;
  color: rgba(0, 0, 0, 0.8);
}
h4 {
  background: #fff;
  padding-top: 12px;
  padding-bottom: 12px;
  margin: 0;
  padding-left: 10px;
  border-radius: 8px 8px 0 0;
  border-bottom: 1px solid #ccc;
}
.change {
  display: flex;
  flex-wrap: wrap;
  background: #fff;
  border-radius: 0 0 8px 8px;
  padding: 20px;
  margin-bottom: 10px;
  > p {
    width: 25%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      // width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      // max-width: 250px;
      flex: 1;
      display: flex;
      line-height: 30px;
    }

    > span {
      display: block;
      width: 120px;
      text-align: right;
      padding-right: 15px;
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
.submits {
  display: flex;
  padding: 15px 20px;
  background: #fff;
  border-radius: 0 0 8px 8px;
  justify-content: flex-end;
  align-items: center;
  > div {
    width: 80px;
    height: 32px;
    text-align: center;
    line-height: 32px;
    color: #fff;
    font-size: 14px;
    border-radius: 4px;
    margin-left: 30px;
    cursor: pointer;
  }
  .save {
    background: #2196f3;
  }
  .nosave {
    background: #999;
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #999 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100%;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
  border: 1px solid #ccc;
}


@media screen and (max-width: 1400px) {
  .change > p {
    width: 33%;
  }
}
</style>